How to Use Printer by Android Programmable

We usually connect the printer to Windows using USB and Wifi, but for some models, the system also needs to install the printer driver. Calling the printer on Android is slightly different.

Before Android 4.4, a third-party solution was needed to implement the printing function, or a protocol for communicating with the printer was implemented. After 4.4, Android introduced the Print class as a printing tool class, such as PrintHelper for printing. Bitmap, PrintPdfDocument to print the document.

Here to print Bitmap as an example, first look at the official documentation, you can see the sample code for printing the resource is as follows

1
2
3
4
5
6
7
private void doPhotoPrint() {
PrintHelper photoPrinter = new PrintHelper(this);
photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.droids);
photoPrinter.printBitmap("droids.jpg - test print", bitmap);
}

Among them, setScaleMode is used to set the mode of printing. It should be noted that many printer service providers do not make this adaptation, so it is likely that the adjustment will not work.

Actual style of print page

The second way is to print the View on the page, here to use the cache to get the elements to be printed, but note that the cache is more expensive, so close the cache as soon as you get the printed View. The sample code is as follows:

1
2
3
4
5
6
7
8
9
private void doPhotoPrint() {
rlPrint.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(rlPhint.getDrawingCache());
rlPrint.setDrawingCacheEnabled(false);

PrintHelper photoPrinter = new PrintHelper(this);
photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);
photoPrinter.printBitmap("droids.jpg - test print", bitmap);
}

Adjustable Style After Printing Layout

The third way is the most customized way, you can use the canvas to customize after the Bitmap is generated, and the image sharing function on many APPs involves this method.

First, we first write a function that converts the View into a Bitmap, as follows

1
2
3
4
5
6
7
8
9
10
11
12
13
private static Bitmap getBitmapFromView(View view) {
//The parameter of the next line is the printing effect adjustment coefficient. The system printer is called. The default printing is at the center position. If it needs to be printed at both ends, it needs to be adjusted by adjusting the height of the Bitmap. The specific adjustment factor needs to be obtained through actual debugging.
int print_resize = 900;
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight() + print_resize, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
view.draw(canvas);
return returnedBitmap;
}

Then similar to the above

1
2
3
4
5
6
7
private void doPhotoPrint(View v) {
Bitmap bitmap = getBitmapFromView(v);

PrintHelper photoPrinter = new PrintHelper(this);
photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);
photoPrinter.printBitmap(new TimeUtil().getTimestampNow(), bitmap);
}

More customization can be achieved by modifying the canvas.

Attention

After Android 4.4, Android and the printer actually connect through Android system printing, that is, as the application layer, do not care about the connection between the printer and the Android system, only need to evoke the system’s print service.

So how do Android and printers connect? On the hardware level, you can connect via USB or Wifi or RJ45. Printer manufacturers should develop corresponding print service drivers for Android systems, which can be downloaded from Play Store or domestic application markets. Then enable the service in the system settings. It’s important to note that if you are connecting over a network, check that both Android and the printer are successfully assigned to the IP.

But in my option, the best way is using the *nix-like system to be a server which connected to a printer, Android just for the client.